<?php

namespace common\models\user;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%user_authent}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $realname 真实姓名
 * @property int $gender 性别:0未知，1男，2女
 * @property int $ict_id 证件类型
 * @property string $idcardno 加密证件号
 * @property string $idcardno_hash 加密证件号（可逆）
 * @property string $photo 人像照
 * @property string $passport_front 证件照正面
 * @property string $passport_reverse 证件照反面
 * @property string $expiry_date 有效期
 * @property string $birthday 出生日期
 * @property int $audit_at 认证通过时间
 * @property string $mark 审核备注
 * @property int $status 认证状态
 * @property int $created_at 提交认证时间
 * @property int $updated_at 更新时间
 */
class UserAuthent extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\relation\UserData;

    const STATUS_AUDITING = 1;
    const STATUS_REFUSE = -1;
    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const GENDER_FEMALE = 2;
    const GENDER_MALE = 1;
    const GENDER_UNKOWN = 0;
    //
    const TYPE_ALLOW = 'allow';
    const TYPE_REJECT = 'reject';
    const TYPE_REFUSE = 'refuse';
    const TYPE_ACCEPT = 'accept';
    const TYPE_RETRY = 'retry';
    //
    const COVER_PATH = '@assets/covers/';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%user_authent}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'gender', 'ict_id', 'audit_at', 'status', 'created_at', 'updated_at'], 'integer'],
            [['realname', 'idcardno'], 'required'],
            [['expiry_date', 'birthday'], 'safe'],
            [['mark'], 'string'],
            [['realname'], 'string', 'max' => 50],
            [['idcardno'], 'string', 'max' => 32],
            [['idcardno_hash', 'photo', 'passport_front', 'passport_reverse'], 'string', 'max' => 255],
            [['user_id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'realname' => '真实姓名',
            'gender' => '性别',
            'ict_id' => '证件类型',
            'idcardno' => '加密证件号',
            'idcardno_hash' => '证件号',
            'photo' => '人像照',
            'passport_front' => '证件照正面',
            'passport_reverse' => '证件照反面',
            'expiry_date' => '有效期',
            'birthday' => '出生日期',
            'audit_at' => '认证通过时间',
            'mark' => '审核备注',
            'status' => '认证状态',
            'created_at' => '提交认证时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getPhotoUrl() {
        return Url::attach($this->photo);
    }

    public function getIcType() {
        return $this->hasOne(\common\models\base\BaseIdcardType::className(), ['id' => 'ict_id']);
    }

    public function setAuditStatus($type, $mark = '') {
        if ($type == static::TYPE_ALLOW) {
            $this->status = static::STATUS_ACTIVE;
            $this->mark = '认证审核通过';
        } elseif ($type == static::TYPE_ACCEPT) {
            $this->status = static::STATUS_AUDITING;
            $this->mark = '管理员正在审核';
        } elseif ($type == static::TYPE_REJECT) {
            $this->status = static::STATUS_REFUSE;
            $this->mark = $mark;
        } elseif ($type == static::TYPE_REFUSE) {
            $this->status = static::STATUS_REFUSE;
            $this->mark = $mark;
        } elseif ($type == static::TYPE_RETRY) {
            $this->status = static::STATUS_DEFAULT;
            $this->mark = '管理员操作重新认证';
        }
    }

    public function getConsoles() {
        $consoles = [
//            [
//                'label' => '修改',
//                'url' => ['update', 'id' => $this->id],
//            ]
        ];
        if ($this->status == static::STATUS_DEFAULT) {
            $consoles[] = [
                'label' => '开始审核',
                'url' => ['/user/authent/audit', 'id' => $this->id],
            ];
        } elseif ($this->status == static::STATUS_AUDITING) {
            $consoles[] = [
                'label' => '通过',
                'url' => ['/user/authent/allow', 'id' => $this->id],
            ];
            $consoles[] = [
                'label' => '驳回',
                'url' => ['/user/authent/reject', 'id' => $this->id],
                'tag' => 'ajaxModal',
            ];
        } elseif ($this->status == static::STATUS_ACTIVE) {
            $consoles[] = [
                'label' => '重新认证',
                'url' => ['/user/authent/retry', 'id' => $this->id],
            ];
        }
        return $consoles;
    }

}
